$(function () {
    //常用jQuery对象
    var entity_datagrid = $("#dictionary_datagrid");
    var entity_dailog = $("#entity_dialog");
    var entity_form = $("#entity_form");

    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {
            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "新增");
        },

        //编辑按钮方法
        edit: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            entity_dailog.dialog("open");
            entity_dailog.dialog("setTitle", "编辑");
            entity_dailog.form("load", row);
        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            entity_form.form("submit", {
                url: "/dictionary/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                            entity_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            entity_dailog.dialog("close");
                        });
                    }
                }
            })
        },


        //删除字典目录
        btn_delete: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示',"请问确定要删除此字典目录吗?", function (r) {
                if (r) {
                    $.get("/dictionary/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                entity_datagrid.datagrid("reload");
                                $("#dictionaryItem_datagrid").datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },

        //刷新页面
        reloadList: function () {
            entity_datagrid.datagrid("reload");
        }

    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    entity_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/dictionary/query.do",
        singleSelect: true,
        pagination: true,
        rownumbers: true,
        onSelect: function () {
            var row = entity_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示","请在右边字典目录选择一条目录","info");
                return;
            }
            $("#dictionaryItem_datagrid").datagrid({url:"/systemDictionaryItem/getItemBySn.do"});
            $("#dictionaryItem_datagrid").datagrid("load",{sn:row.sn});
        },
        columns: [[
            {field: 'id', hidden:true},
            {field: 'sn', title: '字典目录编码',width:50, align: "center"},
            {field: 'name', title: '字典目录名称',width:50, align: "center"},
            {field: 'intro', title: '字典目录简介',width:50, align: "center"}
        ]],
        toolbar: "#tools"
    });


    //编辑添加弹出框设置
    entity_dailog.dialog({
        width: 350,
        height: 200,
        closed: true,
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                entity_dailog.dialog("close");
            }
        }]
    });

    //form表单设置
    entity_dailog.form({
        fit: true
    });

});

